<template>
<div>
<!--  面包屑-->
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    <el-breadcrumb-item>用户列表</el-breadcrumb-item>
  </el-breadcrumb>

<!--  卡片区域-->
  <el-card class="box-card">
    <el-form :inline="true"  class="demo-form-inline">
      <!--    搜索框-->
      <el-form-item >
        <el-input
          placeholder="请输入内容"
          prefix-icon="el-icon-search"
          v-model="queryInfo.query">
        </el-input>
      </el-form-item>
      <el-form-item >
        <el-button type="primary" @click="getAllUser">搜索</el-button>
      </el-form-item >
      <el-form-item >
        <el-button type="primary" @click="addUserVisible=true">添加用户</el-button>
      </el-form-item >
    </el-form>

<!--   表格数据-->
    <el-table
      :data="userList"
      border
      stripe
      style="width: 100%">
      <el-table-column
        type="index">
      </el-table-column>
      <el-table-column
        prop="username"
        label="姓名">
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮箱">
      </el-table-column>
      <el-table-column
        prop="mobile"
        label="电话">
      </el-table-column>
      <el-table-column
        prop="role_name"
        label="角色">
      </el-table-column>
      <el-table-column
        prop="mg_state"
        label="状态">
        <template v-slot="mySlot">
          <el-switch
            v-model="mySlot.row.mg_state"
            active-color="#13ce66"
            @change="userStatusChange(mySlot.row)"
            inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作" >
        <template   v-slot="mySlot" >
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <el-button @click="editDialog(mySlot.row.id)" type="primary" icon="el-icon-search" circle></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">

          <el-button @click="delDialog(mySlot.row)" type="danger" icon="el-icon-delete" circle></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="分配角色" placement="top">
          <el-button @click="role(mySlot.row.id)" type="warning" icon="el-icon-setting" circle></el-button>
          </el-tooltip>
        </template>

      </el-table-column>
    </el-table>

<!--    分页数据-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[2,5,10,20]"
      :page-size="queryInfo.pagesize"
      layout="total,sizes,prev, pager, next,jumper"
      :total="total">
    </el-pagination>


<!--    添加弹框-->
    <el-dialog title="添加用户" v-model="addUserVisible" width="50%">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm"
      class="demo-ruleForm" label-width="70px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username" ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email" ></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="ruleForm.mobile" ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
    <span class="dialog-footer">
      <el-button @click="resetForm('ruleForm')">取 消</el-button>
      <el-button type="primary" @click="addUser('ruleForm')">确 定</el-button>
    </span>
      </template>
    </el-dialog>

<!--    删除用户的弹框-->
    <el-dialog
      title="提示"
      v-model="delDialogVisible"
      width="50%"
      center>
      <span>您确定要删除{{userName}}用户吗</span>
      <template #footer>
    <span class="dialog-footer">
      <el-button @click="delDialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="del">确定</el-button>
    </span>
      </template>

    </el-dialog>

<!--    编辑用户的弹框-->
    <el-dialog title="修改信息" v-model="editDialogVisible" width="50%">
      <el-form :model="updateForm" status-icon :rules="rules" ref="updateForm"
               class="demo-ruleForm" label-width="70px">
        <el-form-item label="用户名" prop="username" >
          <el-input v-model="updateForm.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="updateForm.email" ></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="updateForm.mobile" ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
    <span class="dialog-footer">
      <el-button @click="editDialogVisible=false">取 消</el-button>
      <el-button type="primary" @click="edit">确 定</el-button>
    </span>
      </template>
    </el-dialog>


  </el-card>
</div>
</template>

<script>
import { ElMessage } from 'element-plus'
export default {
  name: "users",
  data(){
    return{
      currentPage4:4,
    userList:[],
      queryInfo:{
        query:'',
        pagenum:1,
        pagesize:5
      },
      total:0,
      addUserVisible:false,
      delDialogVisible:false,
      editDialogVisible:false,
      ruleForm: {
        username:'',
        password:'',
        email:'',
        mobile:''
      },
      updateForm:{
      },
      rules:{
        username:[
          {required:true,message:'请输入账号',trigger:'blur'}
        ],
        password:[
          {required:true,message:'请输入密码',trigger:'blur'}
        ],
        email:[
          {required:true,message:'请输入邮箱',trigger:'blur'}
        ],
        mobile:[
          {required:true,message:'请输入手机',trigger:'blur'}
        ]
      },
      userId:'',
      userName:''
    }
  },
  created() {
    this.getAllUser();
  },
  methods:{

    //获取所有的用户数据
    getAllUser(){
      this.axios.get('users',{
      params:this.queryInfo
      }).then(data=>{
        console.log(data);
        if(data.data.meta.status!=200) return ElMessage.error(data.data.meta.msg);
      //有数据了
        this.userList = data.data.data.users;
        this.total = data.data.data.total;
      }).catch(err=>{
        console.log(err);
      })
    },


    editDialog(id){
      this.editDialogVisible = true;
      this.userId =id;
      this.axios.get(`users/${this.userId}`).then(resp=>{
        this.updateForm =resp.data.data
      }).catch(err=>{
        console.log(err);
      })

    },

    edit(){
      //修改用户数据
      this.axios.put(`users/${this.userId}`,{
        email:this.updateForm.email,
        mobile:this.updateForm.mobile
      }).then(resp=>{
        if(resp.data.meta.status !==200) return ElMessage.error('修改失败');
        ElMessage.success('修改成功');
        this.editDialogVisible = false ;
        this.getAllUser();
      }).catch(err=>{
        console.log(err);
      })

    },


    delDialog(row){
      this.userId=row.id;
      this.userName = row.username;
      this.delDialogVisible = true;
    },

    del(){
      this.axios.delete(`users/${this.userId}`).then(resp=>{
        if(resp.data.meta.status !==200) return ElMessage.error('删除失败');
        ElMessage.success('删除成功');
        this.delDialogVisible = false ;
        this.getAllUser();
      }).catch(err=>{
        console.log(err);
      })
    },
    role(id){
      console.log(id);
    },
   handleSizeChange(val){
      this.queryInfo.pagesize= val;
      this.getAllUser();
    },
    handleCurrentChange(val){
      this.queryInfo.pagenum= val;
      this.getAllUser();
    },

    //添加用户
    addUser(formName){
      this.axios.post('users',this.ruleForm).then(data=>{
        console.log(data);
        if(data.data.meta.status !==201) return ElMessage.error('添加失败');
        ElMessage.success('添加成功');
        this.resetForm(formName);
        this.getAllUser();//重新获取数据
      }).catch(err=>{
        console.log(err);

      });

    },
    //关闭表单所做的事情
    resetForm(formName){
      this.$refs[formName].resetFields();
      this.addUserVisible=false;
    },
    //改变状态
    userStatusChange(row){
    //发送请求
      this.axios.put(`users/${row.id}/state/${row.mg_state}`).then(resp=>{
        console.log(resp);
        ElMessage.success(resp.data.meta.msg);
      }).catch(err=>{
        console.log(err);
      })
    }
  }
}
</script>

<style scoped>
.el-breadcrumb{
  font-size: 20px;
}

.box-card{
  margin-top: 20px;
}
.el-pagination{
  margin-top: 10px;
}
</style>
